package com.jsmile.cloud.tradecenter.express.repo;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.jsmile.cloud.tradecenter.api.req.express.ReqExpressQuery;
import com.jsmile.cloud.tradecenter.express.dao.ExpressCompanyDao;
import com.jsmile.cloud.tradecenter.express.model.ExpressCompany;
import com.jsmile.mall.cache.kit.SpringKit;

import lombok.AllArgsConstructor;

@Component
@AllArgsConstructor
@Transactional(readOnly = true)
public class ExpressCompanySearchRepository extends ServiceImpl<ExpressCompanyDao, ExpressCompany> {

    public static Map<String, ExpressCompany> expressMap = Maps.newHashMap();

    @PostConstruct
    public void init() {
        List<ExpressCompany> expressCompanyList = SpringKit.getBean(ExpressCompanyDao.class).selectByMap(null);
        expressMap = expressCompanyList.stream().collect(Collectors.toMap(ExpressCompany::getCompanyCode, a -> a, (k1, k2) -> k1));
    }

    public List<ExpressCompany> findAll(ReqExpressQuery reqExpressQuery) {
        return this.baseMapper.selectList(getQueryWrapper(reqExpressQuery));
    }

    public Wrapper getQueryWrapper(ReqExpressQuery reqExpressQuery) {
        return new QueryWrapper<ExpressCompany>().lambda().like(Objects.nonNull(reqExpressQuery.getCompanyCode()), ExpressCompany::getCompanyCode, reqExpressQuery.getCompanyCode())
            .orderByDesc(ExpressCompany::getCreateTime);
    }
}
